/*******************************************************************************
 * Copyright 2011 Alexandre Zglav and Morphiastic
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.morphiastic.analysis.filters;

import java.io.IOException;
import java.util.Map;

import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;

import com.morphiastic.analysis.AnalysisComponentDefinition;
import com.morphiastic.utils.EqualsUtil;

/**
 * A filter that stems words (similar to snowball, but with more options).
 *  
 * @author Alex
 *
 */
public class StemmerTokenFilter extends ElasticFilter{

	public static final String LANGUAGE_PARAM_KEY = "language";
	public static final String NAME_PARAM_KEY = "name";
	
	
	public static enum Language {
		
		armenian, basque, catalan, danish, 
		dutch, english, finnish, french, 
		german, german2, hungarian, italian, 
		kp, lovins, norwegian, porter, 
		porter2, portuguese, romanian, russian, 
		spanish, swedish, turkish, minimal_english, 
		possessive_english, light_finish, light_french, 
		minimal_french, light_german, minimal_german, 
		hindi, light_hungarian, indonesian, light_italian, 
		light_portuguese, minimal_portuguese, 
		light_russian, light_spanish, light_swedish
		
	}
	
	
	private Language language;
	
	
	
	public StemmerTokenFilter(String defName){
		this(null,defName);
	}
	
	public StemmerTokenFilter(Map<String,String> params, String defName){
		
		super(ElasticFilterType.stemmer, defName);
		
		if(params!=null){
			
			String languageParam = params.get(LANGUAGE_PARAM_KEY);
			if(languageParam != null && !languageParam.isEmpty()){
				try{
					this.language = Language.valueOf(languageParam);
				}catch (Exception e) {
				}				
			}
			
			if(this.language == null){
				String nameParam = params.get(NAME_PARAM_KEY);
				if(nameParam != null && !nameParam.isEmpty()){
					try{
						this.language = Language.valueOf(nameParam);
					}catch (Exception e) {
					}				
				}
			}
		}
	}
	
	@Override
	public boolean hasCustomParams() {
		 if(this.language!= null )return true;
		 return false;
	}
	
	
	@Override
	public XContentBuilder addToBuilder(XContentBuilder mappingBuilder) throws IOException {
		mappingBuilder.startObject(getDefinitionName())
		.field("type", getComponentSubTypeName());
		if(this.language != null){
			mappingBuilder.field(LANGUAGE_PARAM_KEY, this.language.toString());
		}
		return mappingBuilder;
	}
	
	
	
	
	@Override
	public void setParamsFromSettings(Settings settings) {
		String languageParam = settings.get(LANGUAGE_PARAM_KEY);
		if(languageParam != null && !languageParam.isEmpty()){
			try{
				this.language = Language.valueOf(languageParam);
			}catch (Exception e) {
			}				
		}
		
		if(this.language == null){
			String nameParam = settings.get(NAME_PARAM_KEY);
			if(nameParam != null && !nameParam.isEmpty()){
				try{
					this.language = Language.valueOf(nameParam);
				}catch (Exception e) {
				}				
			}
		}
	}
	
	


	@Override
	public boolean areEquivalentDefinitions(AnalysisComponentDefinition other){
		
		if(other==null)return false;
		StemmerTokenFilter o;
		try {
			o = (StemmerTokenFilter)other;
		} catch (ClassCastException e) {
			return false;
		}
		
		return 
		EqualsUtil.areEqual(this.language, o.language);
	}
	
	
}
